package com.css.msgcenter.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONObject;

import com.css.db.query.TransactionCache;
import com.css.msgcenter.Address;
import com.css.msgcenter.Message;
import com.css.msgcenter.Results;
import com.css.msgcenter.server.model.MsgApp;
import com.css.msgcenter.server.model.MsgMain;
import com.css.msgcenter.server.model.MsgTask;
import com.css.util.Md5Util;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.util.UuidUtil;

public class MessageCenter {
	private static BizLog log = new BizLog(MessageCenter.class);
	
	public static MessageResult push( Message message)
	{
		TransactionCache tx = null;
		try{
			if(StringHelper.isEmpty(message.getTitle())
					||StringHelper.isEmpty(message.getSenderAppCode())
					||message.getAddrlist() == null
					||message.getAddrlist().size() < 1) {
				log.error(Messages.getString("systemMsg.parameterError"));
				return new MessageResult(Results.ERROR, null);
			}
//			准备数据
			MsgApp app = QueryMsg.get(MsgApp.class, Md5Util.getMD5(message.getSenderAppCode()));
			Date cur = (Date) new QueryMsg("select sysdate from dual", true).uniqueResult();
			
			StringBuilder sb1 = new StringBuilder();
			for(int i=0; i<message.getAddrlist().size(); i++) {
				if(i>2)
					break;
				sb1.append(message.getAddrlist().get(i).getUesrname()).append(",");
			}
			if(sb1.length()>0)
				sb1.deleteCharAt(sb1.length()-1);
			if(message.getAddrlist().size()>3)
				sb1.append("...");
			
			Set se = new HashSet();
			for(int i=0; i<message.getAddrlist().size(); i++) {
				Address o1 = message.getAddrlist().get(i);
				if(StringHelper.isNotEmpty(o1.getEmail()))
					se.add(Constants.EMAIL);
				if(StringHelper.isNotEmpty(o1.getIm()))
					se.add(Constants.IM);
				if(StringHelper.isNotEmpty(o1.getPhone()))
					se.add(Constants.PHONE);
				if(StringHelper.isNotEmpty(o1.getLetter()))
					se.add(Constants.LETTER);
				if(StringHelper.isNotEmpty(o1.getWx()))
					se.add(Constants.WX);
			}
			StringBuilder sb2 = new StringBuilder();
			Iterator ite = se.iterator();
			while(ite.hasNext()){
				sb2.append((String) ite.next()).append(",");
			}
			if(sb2.length()>0)
				sb2.deleteCharAt(sb2.length()-1);
			
//			事务开始
			List lsta = new ArrayList();
			MsgMain item = new MsgMain();
			item.setUuid(UuidUtil.getUuid());
			item.setSender(message.getSender());
			item.setReceiver(sb1.toString());
			item.setMsgType(sb2.toString());
			item.setMsgTopic(message.getTitle());
			item.setMsgContent(message.getMessagebody());
			item.setAppId(app.getUuid());
			item.setFinishTime(cur);
			item.setMsgStatus(Constants.STASUS_WAITING);
			item.setTaskFail(0);
			item.setTaskInvalid(0);
			item.setTaskSuccess(0);
			
			
			int sum = 0;
			for(Address o : message.getAddrlist()) {
				if(StringHelper.isNotEmpty(o.getEmail())
						&& StringHelper.isNotEmpty(app.getCheckMsgType())
						&& app.getCheckMsgType().contains(Constants.EMAIL)) {
					MsgTask item2 = new MsgTask();
					item2.setMsgId(item.getUuid());
					item2.setReceiverName(o.getUesrname());
					item2.setCreateTime(cur);
					item2.setMsgStatus(Constants.STASUS_WAITING);
					item2.setFailCount(0);
					item2.setUuid(UuidUtil.getUuid());
					item2.setReceiverId(o.getEmail());
					item2.setMsgType(Constants.EMAIL);
					lsta.add(item2);
					sum++;
				}
				if(StringHelper.isNotEmpty(o.getIm())
						&& StringHelper.isNotEmpty(app.getCheckMsgType())
						&& app.getCheckMsgType().contains(Constants.IM)) {
					MsgTask item2 = new MsgTask();
					item2.setMsgId(item.getUuid());
					item2.setReceiverName(o.getUesrname());
					item2.setCreateTime(cur);
					item2.setMsgStatus(Constants.STASUS_WAITING);
					item2.setFailCount(0);
					item2.setUuid(UuidUtil.getUuid());
					item2.setReceiverId(o.getIm());
					item2.setMsgType(Constants.IM);
					lsta.add(item2);
					sum++;
				}
				if(StringHelper.isNotEmpty(o.getPhone())
						&& StringHelper.isNotEmpty(app.getCheckMsgType())
						&& app.getCheckMsgType().contains(Constants.PHONE)) {
					MsgTask item2 = new MsgTask();
					item2.setMsgId(item.getUuid());
					item2.setReceiverName(o.getUesrname());
					item2.setCreateTime(cur);
					item2.setMsgStatus(Constants.STASUS_WAITING);
					item2.setFailCount(0);
					item2.setUuid(UuidUtil.getUuid());
					item2.setReceiverId(o.getPhone());
					item2.setMsgType(Constants.PHONE);
					lsta.add(item2);
					sum++;
				}
				if(StringHelper.isNotEmpty(o.getLetter())
						&& StringHelper.isNotEmpty(app.getCheckMsgType())
						&& app.getCheckMsgType().contains(Constants.LETTER)) {
					MsgTask item2 = new MsgTask();
					item2.setMsgId(item.getUuid());
					item2.setReceiverName(o.getUesrname());
					item2.setCreateTime(cur);
					item2.setMsgStatus(Constants.STASUS_WAITING);
					item2.setFailCount(0);
					item2.setUuid(UuidUtil.getUuid());
					item2.setReceiverId(o.getLetter());
					item2.setMsgType(Constants.LETTER);
					lsta.add(item2);
					sum++;
				}
				if(StringHelper.isNotEmpty(o.getWx())
						&& StringHelper.isNotEmpty(app.getCheckMsgType())
						&& app.getCheckMsgType().contains(Constants.WX)) {
					MsgTask item2 = new MsgTask();
					item2.setMsgId(item.getUuid());
					item2.setReceiverName(o.getUesrname());
					item2.setCreateTime(cur);
					item2.setMsgStatus(Constants.STASUS_WAITING);
					item2.setFailCount(0);
					item2.setUuid(UuidUtil.getUuid());
					item2.setReceiverId(o.getWx());
					item2.setMsgType(Constants.WX);
					lsta.add(item2);
					sum++;
				}
			}
			
			if(sum < 1) {
				log.error(Messages.getString("systemMsg.parameterError"));
				return new MessageResult(Results.ERROR, null);
			}
			
			item.setTaskSum(sum);
			lsta.add(item);
			tx = new QueryMsg().getTransaction();
			tx.save(lsta);
			tx.commit();
			JSONObject resj = new JSONObject().element("itemId", item.getUuid());
			return new MessageResult(Results.SUCCESS, resj);
		} catch(Exception ex){
			if (tx != null) {
				tx.rollback();
			}
			log.error(ex.getMessage(), ex);
			return new MessageResult(Results.ERROR, null);
		} 
		
	
	}
	
	
}
